package com.qiaofang.dataprocessing.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.qiaofang.dataprocessing.dao.EmployeeDao;
import com.qiaofang.dataprocessing.dao.PropertyDao;
import com.qiaofang.dataprocessing.dao.domain.EmpIdUuid;
import com.qiaofang.dataprocessing.dao.domain.ProRightsInterests;
import com.qiaofang.dataprocessing.dao.domain.PropertyType;
import com.qiaofang.dataprocessing.service.PropertyService;
import com.qiaofang.dataprocessing.stub.bean.ListVerifyCodeDTO;
import com.qiaofang.dataprocessing.stub.bean.ProRightsInterestsDTO;
import com.qiaofang.dataprocessing.stub.bean.PropertyRulesDTO;
import com.qiaofang.dataprocessing.stub.bean.RightsInterests;
import com.qiaofang.dataprocessing.stub.bean.request.PropertyEmpOrderRequest;
import com.qiaofang.dataprocessing.stub.bean.request.PropertyUpDownProcessingRequest;
import com.qiaofang.dataprocessing.stub.bean.request.SysFieldSyncRequest;
import com.qiaofang.dataprocessing.stub.bean.response.HistoryConsignCodeDTO;
import com.qiaofang.dataprocessing.stub.bean.response.PropertyWaterMarkDTO;
import com.qiaofang.dataprocessing.stub.bean.response.WatermarkDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by IDEA.
 *
 * @auther Zym.
 * @company qiaofang
 * @date 2020/11/18 17:18
 */
@Service
@Slf4j
public class PropertyServiceImpl implements PropertyService {
    @Autowired
    private PropertyDao propertyDao;

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<String> getUpPropertyUuidList(PropertyUpDownProcessingRequest processingRequest) {
        List<String> resultList = new LinkedList<>();
        PropertyRulesDTO propertyRules = processingRequest.getPropertyRules();
        List<String> uuidList = processingRequest.getUuidList();
        List<List<String>> partition = Lists.partition(uuidList, 1800);
        for (List<String> list : partition) {
            propertyRules.setUuidList(list);
            resultList.addAll(propertyDao.getUpPropertyUuidList(propertyRules));
        }
        return resultList;
    }

    @Override
    public List<ProRightsInterestsDTO> getProRightsInterests(Date timing) {
        List<ProRightsInterestsDTO> results = new LinkedList<>();
        List<ProRightsInterests> list = propertyDao.getProRightsInterests(timing);
        if (CollectionUtils.isEmpty(list)) {
            return results;
        }
        Map<Long, String> idUuidMap = employeeDao.getEmpIdUuidList().stream().collect(Collectors.toMap(EmpIdUuid::getEmployeeId, EmpIdUuid::getEmployeeUuid, (k1, k2) -> k1));
        for (ProRightsInterests proRightsInterests : list) {
            String propertyUuid = proRightsInterests.getPropertyUuid();
            String rightsInterests = proRightsInterests.getRightsInterests();
            List<RightsInterests> rightsInterestsList;
            try {
                rightsInterestsList = JSONArray.parseArray(rightsInterests, RightsInterests.class);
            } catch (Exception e) {
                rightsInterestsList = new LinkedList<>();
            }
            if (!CollectionUtils.isEmpty(rightsInterestsList)) {
                Iterator<RightsInterests> iterator = rightsInterestsList.iterator();
                while (iterator.hasNext()) {
                    RightsInterests interests = iterator.next();
                    String employeeUuid = idUuidMap.get(interests.getEmpId());
                    if (StringUtils.isEmpty(employeeUuid)) {
                        iterator.remove();
                        continue;
                    }
                    interests.setEmpId(null);
                    interests.setEmployeeUuid(employeeUuid);
                }
            }
            results.add(new ProRightsInterestsDTO(propertyUuid, rightsInterestsList));
        }
        return results;
    }

    @Override
    public List<String> getProEmpOrder(PropertyEmpOrderRequest request) {
        Integer sortRule = request.getSortRule();
        String propertyUuid = request.getPropertyUuid();
        Integer showNum = request.getShowNum();
        List<String> empOrderList = new LinkedList<>();
        switch (sortRule) {
            case 2:
                empOrderList = employeeDao.getInspectionOrder(propertyUuid, showNum);
                break;
            case 3:
                empOrderList = employeeDao.getFollowOrder(propertyUuid, showNum);
                break;
            case 4:
                empOrderList = employeeDao.getSurveyOrder(propertyUuid, showNum);
                break;
        }
        return empOrderList;
    }

    @Override
    public List<ListVerifyCodeDTO> getPropertFieldyByParam() {
        List<ListVerifyCodeDTO> results = new ArrayList<>();
        List<ListVerifyCodeDTO> list = propertyDao.getPropertFieldyByParam();
        if (CollectionUtils.isEmpty(list)) {
            return results;
        }
        Iterator<ListVerifyCodeDTO> iterator = list.iterator();
        while (iterator.hasNext()) {
            ListVerifyCodeDTO next = iterator.next();
            if (StringUtils.isBlank(next.getDelegateNo()) && StringUtils.isBlank(next.getVerifyCode())
                    && StringUtils.isBlank(next.getVerifyQrCode()) && StringUtils.isBlank(next.getVerifyType())) {
                iterator.remove();
                continue;
            }
        }
        results.addAll(list);
        return results;
    }

    @Override
    public List<HistoryConsignCodeDTO> getHistoryConsignCode() {
        return propertyDao.getHistoryConsignCode();
    }

    @Override
    public Map<String, List<String>> getPropertyTypeMap(SysFieldSyncRequest request) {
        List<PropertyType> propertyTypeList = propertyDao.getPropertyTypeList(request.getTiming());
        Map<String, List<String>> resultMap = new HashMap<>();
        for (PropertyType p : propertyTypeList) {
            String propertyType = p.getPropertyType();
            String propertyUuid = p.getPropertyUuid();
            List<String> uuidList = resultMap.get(propertyType);
            if (CollectionUtils.isEmpty(uuidList)){
                uuidList = new LinkedList<>();
            }
            uuidList.add(propertyUuid);
            resultMap.put(propertyType, uuidList);
        }
        return resultMap;
    }

    @Override
    public PropertyWaterMarkDTO getPropertyImgWatermark() {

        List<WatermarkDTO> ImgWatermarkList = propertyDao.getPropertyImgWatermark();
        if (CollectionUtils.isEmpty(ImgWatermarkList)){
            return null;
        }
        PropertyWaterMarkDTO propertyWaterMarkDTO = new PropertyWaterMarkDTO();
        for (WatermarkDTO watermarkDTO : ImgWatermarkList) {
            switch (watermarkDTO.getParaName()){
                case "Watermark":
                    propertyWaterMarkDTO.setWatermark(watermarkDTO.getParaValue());
                    break;
                case "Watermark_type":
                    propertyWaterMarkDTO.setWatermarkType(watermarkDTO.getParaValue());
                    break;
                case "Watermark_align":
                    propertyWaterMarkDTO.setWatermarkAlign(watermarkDTO.getParaValue());
                    break;
                case "Watermark_none":
                    propertyWaterMarkDTO.setWatermarkNone(watermarkDTO.getParaValue());
                    break;
                case "Watermark_content":
                    propertyWaterMarkDTO.setWatermarkContent(watermarkDTO.getParaValue());
                    break;
            }
        }
        //图片水印这里用id来排序取最后一个
        Optional<WatermarkDTO> watermarkOptional = ImgWatermarkList
                .stream().filter(f -> StringUtils.equals(f.getParaName(), "Watermark_url"))
                .sorted(Comparator.comparing(WatermarkDTO::getOptionId).reversed()).findFirst();
        if (watermarkOptional.isPresent()) {
            propertyWaterMarkDTO.setWatermarkUrl(watermarkOptional.get().getParaValue());
        }
        return propertyWaterMarkDTO;
    }
}
